package com.lcsp.client.zoo;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

public class ZKUtil {
	public static ZooKeeper connect(String url, int port) throws IOException, InterruptedException{
		final CountDownLatch signal = new CountDownLatch(1);
		
		ZooKeeper r = new ZooKeeper(url + ":" + port, 6000, new Watcher(){

			@Override
			public void process(WatchedEvent event) {
				signal.countDown();
			}
			
		});
		
		// 等待process方法被执行 被执行了说明已经连上了
		signal.await();
		
		return r;
	}
}
